/**
 * Created by sherr on 2016/10/6.
 */
var ip_bl=true;

var ObjOperate = {
    Create: function () {
        var oo = {};
        var tween;
        var st;
        var bl_canTouch;
        var step;
        var limitAngleX;
        var limitAngleZ;
        var startAngleX;
        var startAngleZ;
        var scene;
        var camera;
        var raycasterMouse = new THREE.Raycaster();
        var mouse = new THREE.Vector2();

        var mouseXPast = 0;
        var mouseX = 0;
        var targetRotationPast = 0;
        var targetRotation = 0;

        var mousePastY = 0;
        var mouseY = 0;
        var targetRotationPastY = 0;
        var targetRotationY = 0;

        var WINNUM;
        var WINMINUS;

        var clickSingle;
        var arr_ipclick;

        var bl_CD;
        var bl_lockCD;
        var CD_Tween;
        var CD;
        var CD_hitTime;
        var CD_minusX=0;
        var CD_minusY=0;


        var bl_MAC;
        var bl_WM;
        var bl_WM_play;

        var bl_mouseUp;

        var macpic1=new THREE.TextureLoader().load("MAC/images/IMacB01.jpg");
        var macpic2=new THREE.TextureLoader().load("MAC/images/IMacB02.jpg");
        var macpic3=new THREE.TextureLoader().load("MAC/images/IMacB03.jpg");

        oo.Initial = function (getMaster,getObjs, getObj, getStartAngleX,getStartAngleZ,getLimitAngleX,getLimitAngleZ, getScene, getCamera) {
            document.addEventListener('mousedown', MD, false);
            document.addEventListener('touchstart', MD, false);
            document.addEventListener('mouseup', MU, false);
            document.addEventListener('touchmove', MVTouch, false);
            document.addEventListener('touchend', TE, false);
            oo.obj = getObj;
            oo.arr_objs=getObjs;
            oo.master = getMaster;

            limitAngleX = getLimitAngleX;
            limitAngleZ = getLimitAngleZ;
            startAngleX=getStartAngleX;
            startAngleZ=getStartAngleZ;

            if(oo.arr_objs==null&&oo.obj.name!="Our_Main") {
                oo.obj.rotation.x = Math.PI / 180 * getStartAngleX;
                oo.obj.rotation.z = Math.PI / 180 * getStartAngleZ;
            }

            targetRotationY=startAngleX*Math.PI/180;
            targetRotation=startAngleZ*Math.PI/180;

            bl_MAC=false;
            bl_WM=false;


            ip_bl=true;

            WINNUM=0;
            WINMINUS=3;
            clickSingle=true;
            arr_ipclick=[];
            for(var a=0;a<14;a++){arr_ipclick[a]=true;}

            bl_CD=false;
            bl_lockCD=false;
            CD_hitTime=0;
            scene = getScene;
            camera = getCamera;

            bl_canTouch = true;
            bl_mouseUp=true;
            step = 1;
            if (tween != null) {
                ween.stop();
            }
        };

        oo.End = function () {
            document.removeEventListener('mousedown', MD, false);
            document.removeEventListener('touchstart', MD, false);
            document.removeEventListener('touchmove', MVTouch, false);
            document.removeEventListener('touchend', TE, false);
            document.removeEventListener('mouseup', MU, false);
        };

        oo.Updating = function () {
            var k;
            if(oo.arr_objs!=null){
                k=oo.arr_objs.length;

            }
            else{
                k=1;
            }

            for(var z=0;z<k;z++) {
                if(k>1){oo.obj=oo.arr_objs[z]}

                if (limitAngleZ == 99) {
                    oo.obj.rotation.z += ( targetRotation - oo.obj.rotation.z ) * 0.1;
                }
                else {
                    var x1 = startAngleZ;
                    var value = limitAngleZ;
                    var minus = targetRotation - oo.obj.rotation.z;
                    if (oo.obj.rotation.z <= Math.PI / 180 * (x1 + value) && oo.obj.rotation.z >= Math.PI / 180 * (x1 - value)) {
                        oo.obj.rotation.z += minus * 0.3;
                    }
                    else if ((oo.obj.rotation.z > Math.PI / 180 * (x1 + value) && minus < 0) || (oo.obj.rotation.z < Math.PI / 180 * (x1 - value) && minus > 0)) {
                        oo.obj.rotation.z += minus * 0.3;
                    }
                    else {
                        targetRotation = oo.obj.rotation.z;
                    }
                }


                if (limitAngleX == 99) {
                    oo.obj.rotation.x += (targetRotationY - oo.obj.rotation.x) * 0.1;
                }
                else {
                    var x1 = startAngleX;
                    var value = limitAngleX;
                    var minus = targetRotationY -oo.obj.rotation.x;
                    if (oo.obj.rotation.x <= Math.PI / 180 * (x1 + value) && oo.obj.rotation.x >= Math.PI / 180 * (x1 - value)) {
                        oo.obj.rotation.x += minus * 0.3;
                    }
                    else if ((oo.obj.rotation.x > Math.PI / 180 * (x1 + value) && minus < 0) || (oo.obj.rotation.x < Math.PI / 180 * (x1 - value) && minus > 0)) {
                        oo.obj.rotation.x += minus * 0.3;
                    }
                    else {
                        targetRotationY = oo.obj.rotation.x;
                    }
                }
            }
        }

        function MVTouch(event) {
            if(!bl_CD) {

                console.log("bl_CD=false!!!")
                mouseX = (event.touches[0].pageX / window.innerWidth) * 2 - 1;
                targetRotation = targetRotationPast + ( mouseX - mouseXPast );

                mouseY = -(event.touches[0].pageY / window.innerHeight) * 2 + 1;
                targetRotationY = targetRotationPastY + (mouseY - mousePastY);
            }


                if(bl_CD&&!bl_lockCD) {
                    if(CD.position.y>155){
                        CD_Tween.stop();
                        CD_Tween=new TWEEN.Tween(CD.material).to({opacity:0},300).start();
                        var value=oo.obj.rotation.z * 180 / Math.PI% 360;

                        if(value>180||(value<180&&value>=0)){targetRotation=(oo.obj.rotation.z * 180 / Math.PI-(value-180))*Math.PI/180}
                        else if(value<-180||(value>-180&&value<0)){targetRotation=(oo.obj.rotation.z * 180 / Math.PI-(value+180))*Math.PI/180}
                        targetRotationY=(oo.obj.rotation.x*180/Math.PI-(oo.obj.rotation.x*180/Math.PI)%180-90)*Math.PI/180;

                        MacDo();
                        if(!bl_lockCD) {
                            WMDo();
                        }
                        bl_lockCD=true;
                        setTimeout(function(){bl_CD=false;},500);
                    }
                    else if(CD.position.y>38.5){
                        // CD.material.opacity=0.5;
                        if(CD_Tween!=null){CD_Tween.stop();}
                        CD_Tween=new TWEEN.Tween(CD.material).to({opacity:0.5},300).start();
                        CD.position.x=(event.touches[0].pageX-158+CD_minusX)/2;
                        CD.position.y=-(event.touches[0].pageY-501+CD_minusY)/2;
                    }
                    else{
                        // CD.material.opacity=1;
                        if(CD_Tween!=null&&CD.material.opacity<1)
                            CD_Tween.stop();
                        CD_Tween=new TWEEN.Tween(CD.material).to({opacity:1},300).start();
                        CD.position.x=(event.touches[0].pageX-158+CD_minusX)/2;
                        CD.position.y=-(event.touches[0].pageY-501+CD_minusY)/2;
                    }




            }
        }
        function TE(event){
            if(bl_CD&&!bl_lockCD) {
                console.log("CD.position.y",CD.position.y)
                if(CD.position.y>38.5){
                    CD_Tween.stop();
                    CD_Tween=new TWEEN.Tween(CD.material).to({opacity:0},300).start();
                    var value=oo.obj.rotation.z * 180 / Math.PI% 360;
                    if(value>180||(value<180&&value>=0)){targetRotation=(oo.obj.rotation.z * 180 / Math.PI-(value-180))*Math.PI/180}
                    else if(value<-180||(value>-180&&value<0)){targetRotation=(oo.obj.rotation.z * 180 / Math.PI-(value+180))*Math.PI/180}
                    targetRotationY=(oo.obj.rotation.x*180/Math.PI-(oo.obj.rotation.x*180/Math.PI)%180-90)*Math.PI/180;
                        MacDo();
                    if(!bl_lockCD) {
                        WMDo();
                    }

                    bl_lockCD=true;

                }

            bl_CD=false;
        }
        }

        function MU(event) {
            document.removeEventListener('mousemove', MVMouse, false);
            if(bl_CD&&!bl_lockCD) {
                console.log("CD.position.y", CD.position.y)
                if (CD.position.y > 38.5) {
                    CD_Tween.stop();
                    CD_Tween = new TWEEN.Tween(CD.material).to({opacity: 0}, 300).start();
                    var value = oo.obj.rotation.z * 180 / Math.PI % 360;
                    if (value > 180 || (value < 180 && value >= 0)) {
                        targetRotation = (oo.obj.rotation.z * 180 / Math.PI - (value - 180)) * Math.PI / 180
                    }
                    else if (value < -180 || (value > -180 && value < 0)) {
                        targetRotation = (oo.obj.rotation.z * 180 / Math.PI - (value + 180)) * Math.PI / 180
                    }
                    targetRotationY = (oo.obj.rotation.x * 180 / Math.PI - (oo.obj.rotation.x * 180 / Math.PI) % 180 - 90) * Math.PI / 180;
                        MacDo();
                    if(!bl_lockCD) {
                        WMDo();
                    }

                    bl_lockCD = true;

                }
                bl_CD = false;

            }
        }

        function MVMouse(event) {
            if(!bl_CD) {
                mouseX = (event.clientX / window.innerWidth) * 2 - 1;
                targetRotation = targetRotationPast + ( mouseX - mouseXPast );

                mouseY = -(event.clientY / window.innerHeight) * 2 + 1;
                targetRotationY = targetRotationPastY + (mouseY - mousePastY);
            }


            if(bl_CD&&!bl_lockCD) {
                console.log("CD.position.y",CD.position.y)
                if(CD.position.y>155){
                    CD_Tween.stop();
                    CD_Tween=new TWEEN.Tween(CD.material).to({opacity:0},300).start();
                    var value=oo.obj.rotation.z * 180 / Math.PI% 360;
                    if(value>180||(value<180&&value>=0)){targetRotation=(oo.obj.rotation.z * 180 / Math.PI-(value-180))*Math.PI/180}
                    else if(value<-180||(value>-180&&value<0)){targetRotation=(oo.obj.rotation.z * 180 / Math.PI-(value+180))*Math.PI/180}
                    targetRotationY=(oo.obj.rotation.x*180/Math.PI-(oo.obj.rotation.x*180/Math.PI)%180-90)*Math.PI/180;



                        MacDo();
                    if(!bl_lockCD) {
                        WMDo();
                    }
                    bl_lockCD = true;
                    setTimeout(function(){bl_CD=false;},500);

                }
                else if(CD.position.y>38.5){
                    // CD.material.opacity=0.5;
                    if(CD_Tween!=null){CD_Tween.stop();}
                    CD_Tween=new TWEEN.Tween(CD.material).to({opacity:0.5},300).start();
                    CD.position.x=(event.clientX-158+CD_minusX)/2;
                    CD.position.y=-(event.clientY-501+CD_minusY)/2;
                }
                else{
                    // CD.material.opacity=1;
                    if(CD_Tween!=null&&CD.material.opacity<1)
                    CD_Tween.stop();
                    CD_Tween=new TWEEN.Tween(CD.material).to({opacity:1},300).start();
                    CD.position.x=(event.clientX-158+CD_minusX)/2;
                    CD.position.y=-(event.clientY-501+CD_minusY)/2;
                }
            }
        }

        function MD(event) {

            bl_mouseUp=false;
            //触摸屏幕变化(细心是硬道理！！)
            if (event.type == "mousedown") {
                //检测碰撞用参数
                mouse.x = (event.clientX / window.innerWidth) * 2 - 1;
                mouse.y = -(event.clientY / window.innerHeight) * 2 + 1;
                // mouse.y = (event.clientX / window.innerWidth) * 2 + 1;
                //旋转物体参数
                document.addEventListener('mousemove', MVMouse, false);
                mouseXPast = mouse.x;
                targetRotationPast = targetRotation;

                mousePastY = mouse.y;
                targetRotationPastY = targetRotationY;
            }
            if (event.type == "touchstart") {
                mouse.x = (event.touches[0].pageX / window.innerWidth) * 2 - 1;
                mouse.y = -(event.touches[0].pageY / window.innerHeight) * 2 + 1;
                //旋转物体参数
                mouseXPast = mouse.x;
                targetRotationPast = targetRotation;

                mousePastY = mouse.y;
                targetRotationPastY = targetRotationY;
            }
            if (bl_canTouch) {
                raycasterMouse.setFromCamera(mouse, camera);
                var IO = scene.children;
                // IO=oo.obj;
                var intersects = raycasterMouse.intersectObjects(IO, true);//加true是为了子对象的判断
                for (var z = 0; z < intersects.length; z++) {
                    if (intersects[z].object.name == "btn_close") {
                        oo.End();
                        oo.master.End();
                    }
                    // console.log( intersects[z].object);
                    //关于我们
                    if ( intersects[z].object.name == "Our_Main"&&oo.obj.material.opacity==1) {
                        window.location.href="http://www.hzgame.com.cn";
                    }
                    //FC
                    if ( intersects[z].object.name == "FC_Main"&&clickSingle) {
                        window.game.pageIn('pageFC');

                        oo.End();
                        oo.master.End();
                        clickSingle=false;
                        setTimeout(function(){clickSingle=true;},50);
                    }
                    //WM分支
                    if ( intersects[z].object.name == "WM_Main"&&bl_lockCD&&clickSingle) {
                        WMDo();
                        clickSingle=false;
                        setTimeout(function(){clickSingle=true;},50);
                    }
                    if(intersects[z].object.name=="CD_Main"&&!bl_lockCD){
                        bl_CD=true;
                        bl_WM=true;
                        CD=intersects[z].object;
                        console.log("MINUS",(event.clientX - 158)/2- CD.position.x);
                    }
                    if(intersects[z].object.name=="DISK_Main"&&!bl_lockCD){
                        bl_MAC=true;
                        bl_CD=true;
                        CD=intersects[z].object;
                        console.log("MINUS",(event.clientX - 158)/2- CD.position.x);
                    }
                    //IP分支
                    if ( intersects[z].object.name == "IP_Main"&&arr_ipclick[0]==true){//进入咳嗽页面！！！！
                        window.game.pageIn('pageText');
                        var zago=intersects[z].object;
                        arr_ipclick[0]=false;//使得可以点击
                        new TWEEN.Tween(zago.material)
                            .to({opacity:0},1000)
                            .easing(TWEEN.Easing.Cubic.Out)
                            .start();
                        new TWEEN.Tween({opacity:0})
                            .to({opacity:1},400)
                            .delay(1000)
                            .easing(TWEEN.Easing.Cubic.Out)
                            .onUpdate(function(){
                                zago.material.opacity=this.opacity;
                            })
                            .easing(TWEEN.Easing.Cubic.Out)
                            .start();
                        setTimeout(function(){
                            oo.End();
                            oo.master.End();

                        },1000)

                    }
                        if ( intersects[z].object.name == "IP_Zago" &&arr_ipclick[intersects[z].object.material.name]==true ) {
                        for(var r=0;r<16;r++){
                            // oo.obj.matrixWorld.elements[r]=r;
                        }
                        // oo.obj.matrixWorld.elements[0]=10;
                        // console.log("oo.object",oo.obj.matrixWorld.elements);

                        // oo.obj.applyMatrix(oo.obj.matrixWorld);
                        var zago=intersects[z].object;
                        // console.log("IO",zago);

                        arr_ipclick[intersects[z].object.material.name]=false;
                        console.log(intersects[z].object.material.name,arr_ipclick[intersects[z].object.material.name]);

                        setTimeout(function(){arr_ipclick[zago.material.name]=true;},1600);


                        new TWEEN.Tween(zago.material)
                            .to({opacity:0},1000)
                            .easing(TWEEN.Easing.Cubic.Out)
                            .start();
                        new TWEEN.Tween({opacity:0})
                            .to({opacity:1},600)
                            .delay(1000)
                            .easing(TWEEN.Easing.Cubic.Out)
                            .onUpdate(function(){
                                zago.material.opacity=this.opacity;
                            })
                            .easing(TWEEN.Easing.Cubic.Out)
                            .start();

                            // new TWEEN.Tween({x:-10,y:10,angle:-90,scale:1})
                            //     .to({x:10,y:-10,angle:90,scale:0},1000)
                            //     .repeat(5)
                            //     .delay(1000)
                            //     .onUpdate(function(){
                            //         intersects[z].object.rotation.z=this.angle*Math.PI/180;
                            //
                            //         //  IO.scale.set(this.scale,this.scale,this.scale);
                            //     })
                            //     .start();
                    }
                    //WIN95分支
                    if (intersects[z].object.name == "WIN_Main"&&clickSingle) {
                        clickSingle=false;
                        if(intersects[z].object.material.opacity==1&&WINNUM<oo.arr_objs.length-WINMINUS) {
                            var bl=true;
                            while(bl) {
                                if (oo.arr_objs[WINNUM].name == "WINDOW") {
                                    oo.arr_objs[WINNUM].children[0].children[0].material.opacity = 0;
                                    oo.arr_objs[WINNUM].scale.set(0.01,0.01,0.01);
                                    new TWEEN.Tween(oo.arr_objs[WINNUM].scale)
                                        .to({x:0.09-WINNUM*0.001,y:0.09-WINNUM*0.001,z:0.09-WINNUM*0.001},400)
                                        .easing(TWEEN.Easing.Cubic.Out)
                                        .start();
                                    new TWEEN.Tween(oo.arr_objs[WINNUM].children[0].children[0].material)
                                        .to({opacity:1},400)
                                        .easing(TWEEN.Easing.Cubic.Out)
                                        .start();
                                    bl=false;
                                }
                                else{
                                    WINMINUS--;
                                }
                                WINNUM++;
                            }
                        }
                       else  if(WINNUM>=oo.arr_objs.length-WINMINUS&&WINNUM<oo.arr_objs.length+100){
                            WINNUM+=200;
                            for(var i=0;i<oo.arr_objs.length;i++){
                                if(oo.arr_objs[i].name=="WIN03"){
                                    oo.master.Blue();
                                    break;
                                }
                            }
                        }
                       setTimeout(function(){clickSingle=true;},100);
                    }
                    //MAC电脑分支
                    if (intersects[z].object.name == "MAC_Main"&&bl_lockCD) {
                       MacDo();
                    }
                }
            }
        }
       function MacDo(){
           if(bl_MAC) {
               if (step == 4) {
                   step = 1;
               }
               ;
               bl_canTouch = false;
               if (tween != null) {
                   tween.stop();
               }
               tween = new TWEEN.Tween(oo.obj.children[1].children[0].material)
                   .to({opacity: 0}, 300)
                   .delay(200)
                   .easing(TWEEN.Easing.Cubic.Out)
                   .start();


               if (st != null) {
                   clearTimeout(st);
               }


               st = setTimeout(function () {
                   // if (step == 0) {
                   //     oo.obj.children[1].children[0].material.map = new THREE.TextureLoader().load("MAC/images/IMacB01.jpg");
                   // }
                   if (step == 1) {
                       oo.obj.children[1].children[0].material.map = macpic1;
                   }
                   if (step == 2) {
                       oo.obj.children[1].children[0].material.map = macpic2;
                   }
                   if (step == 3) {
                       oo.obj.children[1].children[0].material.map = macpic3;
                   }
                   tween.stop();
                   tween = new TWEEN.Tween(oo.obj.children[1].children[0].material)
                       .to({opacity: 1}, 500)
                       .easing(TWEEN.Easing.Cubic.InOut)
                       .start();
                   setTimeout(function () {
                       bl_canTouch = true;
                   }, 300);
                   step++;
               }, 400)
           }
       };
       function WMDo(){
           if(bl_WM) {
               if(!bl_WM_play){
                   bl_WM_play=true;
                    window.game.doIn('pagePlaySound');
               }
               else{
                   bl_WM_play=false;
                    window.game.doIn('pageStopSound');
               }

           }
       };

        return oo;
    }
};